package com.cy.member;

import java.awt.Point;
import java.util.ArrayList;

import com.cy.controller.MasterController;
import com.cy.member.parent.ChineseChessman;
/**
 * 将
 * @author CY
 */
public class Jiang extends ChineseChessman {
	
	/**
	 * @param who true红色，false黑色
	 */
	public Jiang(boolean who) {
		
		if(who) {//红车
			this.setBackground("image/红将.GIF");
		} else {//黑车
			this.setBackground("image/黑将.gif");
		}
		//生成棋子
		this.generateChessman(who);
	}

	/**
	 * 将的移动策略
	 * 先判断是否直线移动步长为1.x坐标3-5
	 * 然后分别为上下将设置y坐标的限制
	 */
	@Override
	public boolean move(Point site, ArrayList<Point> points) {
		if(MasterController.gameType) {
			return jingdian(site, points);
		} else {
			return liuxing(site);
		}
	}

	/**
	 * 经典模式
	 * @param site
	 * @param points
	 * @return
	 */
	private boolean jingdian(Point site, ArrayList<Point> points) {
		//起点坐标
		Point origin = getChessman().getSite();
		//要求直线运动，而且步长为1,3<=x<=5
		if(((origin.x == site.x && Math.abs(origin.y-site.y) == 1) || (origin.y == site.y && Math.abs(origin.x-site.x)==1)) && site.x >= 3 && site.x <= 5){
			if(origin.y > 5) { //下面的将
				if(site.y >= 7 && site.y <= 9) {//可以走
					getChessman().setSite(site);
					return true;
				} 
			} else { //上面的将
				if(site.y >= 0 && site.y <= 2) {//可以走
					getChessman().setSite(site);
					return true;
				} 
			}
		}
		
		//判断是否存在明将
		ChineseChessman cc = MasterController.getChessboard().getChineseChessman(site);
		if(cc != null && cc.getClass().toString().equals(this.getClass().toString()) && points.size() == 2){
			getChessman().setSite(site);
			return true;
		} 
		return false;
	}
	
	/**
	 * 流行模式
	 * @param site
	 * @return
	 */
	private boolean liuxing(Point site) {
		//起点坐标
		Point origin = getChessman().getSite();
		//要求单步走直线
		if((Math.abs(origin.x - site.x) == 1 && Math.abs(origin.y - site.y) == 0) || (Math.abs(origin.x - site.x) == 0 && Math.abs(origin.y - site.y) == 1)) {
			getChessman().setSite(site);
			return true;
		}
		return false;
	}
}
